<?php

/*
* 上传案件
*/

class PubliccaseauditController extends Controller_Base
{
    public function init()
    {
        parent::init();

    }
    /**
     * 录入案件审核列表页
     */
    public function indexAction()
    {
        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $limit = intval($request->getQuery('limit', 10));
        $mediationOrganId  = intval($request->getQuery('mediationOrganId', 0));
        $importRecordId = intval($request->getQuery('importRecordId', ''));
        $litigantName = $request->getQuery('litigantName', '');
        $cardId = $request->getQuery('cardId', '');
        //检测批次解析和提交
        $where = $this->getWhere();
        array_push($where,"caseStatus = 4");

        if($mediationOrganId){
            array_push($where,"mediationOrganId = ".$mediationOrganId);
        }
        if($importRecordId){
            array_push($where,"importRecordId = ".$importRecordId);
        }
        if($litigantName){
            array_push($where,"litigantName = '{$litigantName}'");
        }
        if($cardId){
            if(strlen((string)$cardId) == 6) {
                array_push($where, "cardId LIKE '%{$cardId}'");
            }else{
                array_push($where, "cardId = '{$cardId}'");

            }
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if($add_begin_end){
            $query['add_begin_end'] = $add_begin_end;
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "addTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "addTime >= {$begin_date}");
            }
        }
        $query = [
            'importRecordId' => $importRecordId ? $importRecordId : '',
            'litigantName' => $litigantName,
            'cardId' => $cardId,
            'mediationOrganId' => $mediationOrganId,
            'add_begin_end' => $add_begin_end,
            'limit' => $limit,
        ];

        $page = $page ? $page : 1;
        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => ($page - 1) * $limit,
            'order' => 'id DESC'
        );

        $arrList = Module_Mediate_PublicCase::getInstance()->getAllBySearch([],$seacher,true);
        $count = Module_Mediate_PublicCase::getInstance()->getAllBySearch(array("count(*) as num","sum(debtTarget) as debtTotal","sum(debtPrincipal) as debtAmount",'COUNT(DISTINCT cardId) as groupTotal'), array('where' => $where), true);

        $total = $count[0]['num'];
        $totalPage = ceil($total / $limit);
        //赋值调解部门
        $this ->getMediationOrgan();


        SmartyAdapter::assign('debtTotal', empty($count[0]['debtTotal']) ? 0 : $count[0]['debtTotal']);
        SmartyAdapter::assign('debtAmount',  empty($count[0]['debtAmount']) ? 0 : $count[0]['debtAmount']);
        SmartyAdapter::assign('groupTotal', empty($count[0]['groupTotal']) ? 0 : $count[0]['groupTotal']);
        SmartyAdapter::assign('arrList', $arrList);
        SmartyAdapter::assign('disputeType',  Module_Mediate_Case::$disputeType);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('page_query', http_build_query($query));

    }
    /**
     * 审核状态,驳回通过
     */
    public function auditAction()
    {
        $request = $this->getRequest();
        $caseId = ($request->getPost('case_id', []));
        $auditStatus = ($request->getPost('audit_status', ''));
        if (empty($caseId) || empty($auditStatus) || !in_array($auditStatus, ['pass', 'reject'])) {
            AjaxTool::returnJsonData(4001);
        }
        //查询匹配到调解员的案件，跳过分配


        //查询需要分配的的案件
        $allocate_caseId = explode(',',$caseId);
        //分配
        if($auditStatus == 'pass'){
            $y = date('Y');
            $mediationNum = SmartyAdapter::getTplVars('mediationNum');
            $saveData = [
                'caseStatus' => 1,
                'updateTime' => time()
            ];
            $res = Module_Mediate_PublicCase::getInstance()->saveByIds($saveData, implode(',',$allocate_caseId));
            @Module_Mediate_PublicCase::getInstance()->saveByIds(['mediationNum' => "CONCAT('（{$y}）{$mediationNum}',importRecordId,'-',id,'号')"], $caseId,["mediationNum=''"]);
        }else{
            $saveData = [
                'caseStatus' => 5,
                'updateTime' => time()
            ];
            $res = Module_Mediate_PublicCase::getInstance()->saveByIds($saveData, $caseId);
        }
        //分配成功，添加日志记录
        if(!$res){
            AjaxTool::returnJsonData(400, [], '审核失败' );
        }
        AjaxTool::returnJsonData(200, [], '审核成功' );


    }
}
